﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Diagnostics;

using RaisingStudio.SmallProgram.Library.Internal;

namespace RaisingStudio.SmallProgram.Library
{
    [SmallProgramType]
    public static class Dictionary
    {
        private const string _englishGuid = "{FEF89077-4F4D-4803-A8BF-228083F70EAA}";
        private const string _frenchGuid = "{92DF305E-677C-43D1-9FC7-B2A3D08DCA5C}";
        private const string _spanishGuid = "{FDB3E101-5014-44BE-AA64-BD0E5B55B3B7}";
        
        private const string _queryXml = "<QueryPacket xmlns='urn:Microsoft.Search.Query' revision='1' >\r\n                                   <Query domain='{2}'>\r\n                                     <Context>\r\n                                       <QueryText type='STRING' language='en-us' >{0}</QueryText>\r\n                                       <LanguagePreference>en-us</LanguagePreference>\r\n                                     </Context>\r\n                                     <OfficeContext xmlns='urn:Microsoft.Search.Query.Office.Context' revision='1'>\r\n                                       <UserPreferences>\r\n                                         <ParentalControl>false</ParentalControl>\r\n                                       </UserPreferences>\r\n                                       <ServiceData>{1}</ServiceData>\r\n                                       <ApplicationContext>\r\n                                         <Name>Microsoft Office Word</Name>\r\n                                         <Version>(14.0.3524)</Version>\r\n                                       </ApplicationContext>\r\n                                       <QueryLanguage>en-us</QueryLanguage>\r\n                                       <KeyboardLanguage>en-us</KeyboardLanguage>\r\n                                    </OfficeContext>\r\n                                   </Query>\r\n                                 </QueryPacket>";

        public static Primitive GetDefinition(Primitive word)
        {
            return GetDefinition((string)word, "EDICT", "{FEF89077-4F4D-4803-A8BF-228083F70EAA}");
        }

        public static Primitive GetDefinitionInFrench(Primitive word)
        {
            return GetDefinition((string)word, "FDICT", "{92DF305E-677C-43D1-9FC7-B2A3D08DCA5C}");
        }

        private static string GetDefinition(string word, string serviceCode, string langGuid)
        {
            StringBuilder stringBuilder = new StringBuilder();
            try
            {
                ResearchSoapClient officeResearch = new ResearchSoapClient();
                string s = officeResearch.Query(string.Format("<QueryPacket xmlns='urn:Microsoft.Search.Query' revision='1' >\r\n                                   <Query domain='{2}'>\r\n                                     <Context>\r\n                                       <QueryText type='STRING' language='en-us' >{0}</QueryText>\r\n                                       <LanguagePreference>en-us</LanguagePreference>\r\n                                     </Context>\r\n                                     <OfficeContext xmlns='urn:Microsoft.Search.Query.Office.Context' revision='1'>\r\n                                       <UserPreferences>\r\n                                         <ParentalControl>false</ParentalControl>\r\n                                       </UserPreferences>\r\n                                       <ServiceData>{1}</ServiceData>\r\n                                       <ApplicationContext>\r\n                                         <Name>Microsoft Office Word</Name>\r\n                                         <Version>(14.0.3524)</Version>\r\n                                       </ApplicationContext>\r\n                                       <QueryLanguage>en-us</QueryLanguage>\r\n                                       <KeyboardLanguage>en-us</KeyboardLanguage>\r\n                                    </OfficeContext>\r\n                                   </Query>\r\n                                 </QueryPacket>", word, serviceCode, langGuid));
                XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(s));
                xmlTextReader.WhitespaceHandling = WhitespaceHandling.Significant;
                if (xmlTextReader.ReadToDescendant("Content"))
                {
                    XmlReader xmlReader = xmlTextReader.ReadSubtree();
                    while (xmlReader.Read())
                    {
                        string name;
                        if ((name = xmlReader.Name) != null)
                        {
                            if (name == "Heading" || name == "Line" || name == "P")
                            {
                                stringBuilder.AppendLine();
                                continue;
                            }
                            if (name == "Char" || name == "Text")
                            {
                                if (xmlReader.NodeType == XmlNodeType.Element)
                                {
                                    stringBuilder.Append(" ");
                                    continue;
                                }
                                continue;
                            }
                        }
                        if (xmlReader.NodeType == XmlNodeType.Text)
                        {
                            stringBuilder.Append(xmlReader.Value);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            return stringBuilder.ToString();
        }
    }
}

